package com.example.easycharge.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.easycharge.entity.Bill;
import com.example.easycharge.entity.Customer;
import com.example.easycharge.repository.CustomerRepository;
import com.example.easycharge.service.BillService;
import com.example.easycharge.service.ChargeService;
import com.example.easycharge.service.CustomerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Tag(name = "用户接口组")
@RestController
@RequestMapping("user")
public class CustomerController {
    @Autowired
    private ChargeService chargeService;
    @Autowired
    private BillService billService;
    @Autowired
    private CustomerService customerService;

    @Operation(description = "查询当前用户申请充电的车辆的信息（预计等待时间、电量百分比等）")
    @GetMapping("/totalVehicle")
    public List<JSONObject> getAllVehicleInfo(HttpServletRequest httpServletRequest) {
        long customerId = (long) httpServletRequest.getAttribute("customerId");
        return chargeService.getAllVehicleInfo(customerId);
    }

    @Operation(description = "查询某个待充电/已充电车辆的信息")
    @GetMapping("/vehicle/{vehicleId}")
    public JSONObject getSigVehicleInfo(@PathVariable("vehicleId") long vehicleId) {
        return chargeService.getSigVehicleInfo(vehicleId);
    }

    @Operation(description = "查询当前用户所有账单信息")
    @GetMapping("/bill")
    public List<Bill> getAllBill(HttpServletRequest httpServletRequest) {
        long customerId = (long) httpServletRequest.getAttribute("customerId");
        return billService.getAllBillByCustomerId(customerId);
    }

    @Operation(description = "修改用户信息的接口，可以改动的值有【userPassword、userNickName、userPhone、autoSelect、autoFix】，试图更改无法改动的或者不存在的数据将会返回错误码")
    @PutMapping("/setting/{changeItem}/{newValue}")
    public JSONObject updateUserDetail(HttpServletRequest httpServletRequest,
                                       @PathVariable("changeItem") String changeItem,
                                       @PathVariable("newValue") String newValue) {
        long customerId = (long) httpServletRequest.getAttribute("customerId");
        return customerService.updateUserDetail(customerId, changeItem, newValue);
    }

}
